/*
 * @Description: 通过并发布。新闻、公告
 */
import { useBaseInfoWithOut } from '@/store/modules/base-info';
import globalData from '@/plugins/data';
import globalMethods from '@/plugins/methods';
//提交
import {canTransSecretLevel} from "@/helper/secret-check";

const useBaseInfo = useBaseInfoWithOut();
function alertMsg(msg='checkRole.no.msg'){
    globalMethods.warning({
        title: globalMethods.i18n('common.system.hint.label'),
        content: globalMethods.i18n(msg),
    });
}

export default async function publish(btn, resolve) {
    const {summary, affair} = useBaseInfo.businessData;
    let nodePolicy = affair?.nodePolicy||'';
    let isNews = false;
    if (nodePolicy === 'newsaudit') {
        if (!$.ctx.hasPlugin("news")){
            alertMsg();
            return resolve(false);
        }
        let callBackendMethod = window.callBackendMethod("newsIssueManager","hasAuthority");
        if (!callBackendMethod){
            alertMsg('news.issue.notAuthority');
            return resolve(false);
        }
        isNews = true;
    } else if (nodePolicy === 'bulletionaudit') {
        if (!$.ctx.hasPlugin("bulletin")){
            alertMsg();
            return resolve(false);
        }
        let callBackendMethod = window.callBackendMethod("bulIssueManager","hasAuthority");
        if (!callBackendMethod){
            alertMsg('bulletin.not_purview');
            return resolve(false);
        }
        isNews = false;
    } else {
        //不是新闻公告审批，直接通过
        return resolve(true);
    }
    //如果不能该转发就提示
    if (!(await canTransSecretLevel())) {
        return resolve(false);
    }
    
    //判断当前登陆用户是否有发布权限
    // $.alert($.i18n('news.issue.notAuthority'));

    //区分发布新闻、公告
    const url = isNews
        ? `${globalData.ctxPath}/newsIssue.do?method=preIssue&summaryId=${globalData.summaryId}&affairId=${globalData.affairId}&bodyType=${summary.bodyType}&entry=0`
        : `${globalData.ctxPath}/bulIssue.do?method=preIssue&summaryId=${globalData.summaryId}&affairId=${globalData.affairId}&bodyType=${summary.bodyType}&formContent=false&entry=0&outworkerPanel=1`;
    //阻止连续点击
    let isSubmit = false;
    const dialog = globalMethods.dialog({
        url,
        width: 615,
        height: 451,
        bodyStyle:'padding:0',
        title: $.i18n('news.preIssue.select.scope'),
        checkMax: true,
        onCancel() {
            dialog.close();
            resolve(false);
        },
        onConfirm: () => {
            if (isSubmit) return;
            const result = dialog.getValue();
            //模块里面校验通过了才能继续
            if (!result.length) return;
            isSubmit = true;
            console.log(result);
            let baseInfo = useBaseInfo.baseInfo;
            //传递数据
            btn.getThirdpartParamData = {
                key: isNews ? 'news' : 'bulletin',
                transObject: {
                    result,
                    rightId: baseInfo.zwIframe.cap4.rightId,
                    bodyType: baseInfo.businessData.summary.bodyType,
                },
            };

            //新闻公告发布之后直接关闭
            dialog.close();
            resolve(true);

            // submit.call(this, btn);
            // dialog.close();
        },
    });
}
